package cn.dly.login.controller;

import cn.dly.login.utils.Container;
import cn.dly.login.vo.MsgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

@RestController
@Api(tags = "短信验证码")
@RequestMapping("/sys/msg")
@CrossOrigin
public class MessageLogController {
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    private static final String addr = "http://api.sms.cn/sms/";
    private static final String userId = "dante173";//填写云信登录账户名
    private static final String pwd = md5("bi753951angdante173"); //填写云信MD532位接口密码
    private static final String encode = "utf8";
    private Logger logger = LoggerFactory.getLogger(MessageLogController.class);

    @ApiOperation("发送短信验证码")
    @RequestMapping(value = "/send", method = {RequestMethod.POST})
    public void exportaa(MsgVo msgvo) throws IOException {
        String mobile = msgvo.getMobile();
        String id=msgvo.getId();
        Random random = new Random();
        String result = "";
        for (int i = 0; i < 6; i++) {
            result += random.nextInt(10);
        }
        String codea = "code";
        String code = "\"" + codea + "\"";
        String content = "";
        //组建请求
        System.out.println(id);
        if (id.equals("1")) {
            redisTemplate.opsForValue().set(mobile, result);
            content = "&content=" + "手机注册验证码为" + result + "，请尽快填写以完成用户注册【昆明理工大学城市学院捐赠平台】";
        } else if (id.equals("2")) {
            redisTemplate.opsForValue().set(mobile, result);
            content = "&content=" + "手机注册验证码为" + result + "，请尽快填写以完成用户注册【昆明理工大学城市学院捐赠平台】";
        }
        String straddr = addr +
                "?ac=send&uid=" + userId +
                "&pwd=" + pwd +
                "&mobile=" + mobile +
                "&encode=" + encode +
                content;
//                "&content=" + "{"+code+":"+resulta+"}";
//                "手机注册验证码为{"+resulta+"}，请尽快填写以完成用户注册【昆明理工大学城市学院捐赠平台】"
//                +"&template=100006";
        System.out.println(straddr);
        StringBuffer sb = new StringBuffer(straddr);
        //发送请求
        URL url;
        String inputline = "";
        try {
            url = new URL(sb.toString());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    url.openStream()));

            //返回结果
            inputline = in.readLine();
            System.out.println(inputline + "a");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Response:" + inputline);
    }

    public static String md5(String plainText) {
        StringBuffer buf = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }

}

